package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.comment.domain.Wenda;
import cn.wolfcode.luowowo.comment.domain.WendaComment;
import cn.wolfcode.luowowo.comment.service.IWendaCommentService;
import cn.wolfcode.luowowo.comment.service.IWendaService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Random;
import java.util.UUID;

@Controller
@RequestMapping("/wendaDetail")
public class WenDetailController {

    @Reference
    private IWendaService wendaService;
    @Reference
    private IWendaCommentService wendaCommentService;
    @Reference
    private IUserInfoService userInfoService;

    @RequestMapping("wendaDetail")
    public String wendaDetail(String id, Model model, @LoginUser UserInfo userInfo) {
        //answers : 查找提问的评论
        List<WendaComment> list = wendaCommentService.listByWendaId(id);
        model.addAttribute("answers", list);
        //userinfo : 用户信息
        if (userInfo != null) {
            model.addAttribute("userinfo", userInfo);
        }
        //wenda:阅读数增加阅读数
        Wenda wenda = wendaService.getWenda(id);
        Integer viewnum = wenda.getViewnum();
        wenda.setViewnum(viewnum+1);
        AjaxResult save = wendaService.save(wenda);
        model.addAttribute("wenda", wenda);
        //answernum : 评论数
        model.addAttribute("answernum", wenda.getAnswernum());
        //isFollow : 关注按钮的回显
        if (userInfo != null) {
            model.addAttribute("isFollow",wendaService.isFavor(id,userInfo.getId()));
        }
        return "wenda/wendaDetail";
    }

    @RequestMapping("/commentAll")
    @ResponseBody
    public AjaxResult commentAll(String id) {
        AjaxResult result = new AjaxResult();
        List<WendaComment> wendaComments = wendaCommentService.listByWendaId(id);
        if (wendaComments == null) {
            result.mark("没有回答");
        }
        return result;
    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    public AjaxResult commentAdd(WendaComment wendaComment, @LoginUser UserInfo userInfo, Model model) {
        if (userInfo == null) {
            return new AjaxResult().mark("请先登录");
        }
        //填充用户ID/昵称/等级/头像
        wendaComment.setUserId(userInfo.getId());
        wendaComment.setUsername(userInfo.getNickname());
        wendaComment.setLevel(userInfo.getLevel());
        wendaComment.setHeadUrl(userInfo.getHeadImgUrl());

        //java生成随机数0或1，只能是0，1,用户判定是否金牌数
        Random r=new Random();
        int i = r.nextInt(10);
        if (i > 4) {
            wendaComment.setGoldAnswer(WendaComment.JP_GOLDANSWER);//是金牌
            //添加获得金牌的用户ID
            Wenda wenda = wendaService.getWenda(wendaComment.getWendaId());
            List<Long> goldAnswerList = wenda.getGoldAnswerList();
            goldAnswerList.add(wendaComment.getUserId());
            wendaService.save(wenda);
        }

        //保存社区问答的评论到Mongdb
        AjaxResult result = wendaCommentService.save(wendaComment);
        if (result.isSuccess()) {
            //每次回复,通过ObjecId查询出Wenda对象,设置answernum(评论数)+1
            String wendaId = wendaComment.getWendaId();
            Wenda wenda = wendaService.getWenda(wendaId);
            //拿到提问的回答数
            Integer answernum = wenda.getAnswernum()+1;
            wenda.setAnswernum(answernum);
            wendaService.save(wenda);
            UserInfo info = userInfoService.get(wenda.getUserId());

            info.setRankingNum(wenda.getAnswernum());
            userInfoService.updateRanking(info);
        }

        return result;
    }

    @RequestMapping("favor")
    @ResponseBody
    public AjaxResult favor(String wendaId, @LoginUser UserInfo userInfo) {
        return wendaService.favor(wendaId, userInfo);
    }
}
